<%'  ------------------------------------------------------------
  '  _docnavigate.asp 
  '  ------------------------------------------------------------
  '  VCS INFO:
  '  
  '  $Revision:   1.31  $
  '  $Date:   03 May 2000 14:25:54  $
  '  $Author:   vpham  $
  '  $Workfile:   _docnavigate.asp  $
  '  ------------------------------------------------------------
  '  All Rights Reserved.  Copyright (c) 1988-1998 FileNET Corp.
  '  ------------------------------------------------------------
  '%>
<!-- #include file="islogon.asp" -->
<%
CRLF = CStr(chr(13)) + CStr(chr(10))

FullURL = Request.ServerVariables("PATH_INFO")
p1 = InStrRev(FullURL, "/")
BaseURL = Left(FullURL, p1 - 1)

'SERVER_URL contains the ServerProtocol + ServerName [+ ServerPort]
ServerURL = Request.ServerVariables("SERVER_URL")
if Len(ServerURL) = 0 then
	'If not found, then compose each individual parts that we need for ServerURL.

	ServerName = Request.ServerVariables("HTTP_HOST")
	if Len(ServerName) = 0 then
		ServerName = Request.ServerVariables("SERVER_NAME")
	end if

	'SERVER_HOST might contain port number
	pos = InStr(ServerName, ":")
	if pos > 0 then
		'If found, strip off the port number.
		ServerName = Left(ServerName, pos - 1)
	end if

	'Using the SERVER_PORT and SERVER_PORT_SECURED is more reliable then getting the
	'port number from SERVER_HOST.
	ServerPort = Request.ServerVariables("SERVER_PORT")
	ServerPortSecure = Request.ServerVariables("SERVER_PORT_SECURE")
	if ServerPortSecure then
		if ServerPort <> "443" then
			ServerPort = ":" + ServerPort
		else
			ServerPort = ""
		end if
		ServerProtocol = "https://"
	else
		if ServerPort <> "80" then
			ServerPort = ":" + ServerPort
		else
			ServerPort = ""
		end if
		ServerProtocol = "http://"
	end if

	'Compose the ServerURL with each part obtained earlier
	ServerURL = ServerProtocol + ServerName + ServerPort
end if


'Panagon IDM Web application uses the Session variable LibraryLogonID to keep track of
'whether the user has logged on.
'Please see logoncall.asp to see how the Session variable LibraryLogonID is set.
LogonId = Session("LibraryLogonID")

'Create a Library object, reattach LogonID to get the SystemType and SystemName
set LibraryObj = Server.CreateObject("IDMObjects.Library")
LibraryObj.LogonID = LogonID
SystemType = LibraryObj.SystemType
LibraryName = LibraryObj.Name

'Check to make sure the document exists
DocID = Request.QueryString("DocID")

On Error Resume Next
set DocObj = LibraryObj.GetObject(Application("idmObjTypeDocument"), DocID)
if Len(Err.Description) then
	Response.Expires = 0
	Response.Status = "404 Not Found"
	Response.ContentType = "text/plain"
	Response.Write "Document not found."
	Response.End
end if

'For DS system, if CheckOut is passed in, then check out the document also.
if SystemType = Application("idmSysTypeDS") then
    Checkout = Request.QueryString("Checkout")
    if Checkout = "1" then
        set VersObj = DocObj.Version
        On Error Resume Next
        VersObj.CheckoutNoCopy
        if Len(Err.Description) then
            Response.Expires = 0
            Response.Status = "404 Not Found"
            Response.ContentType = "text/plain"
            Response.Write "Checkout failed: "
            Response.Write Err.Description
            Response.End
        end if
    end if
end if

DocLocationStr = ""
DocFormatStr   = ""

if SystemType = Application("idmSysTypeIS") then
	'For Image Services, check that the page is within range.
	Page = Request.QueryString("StartPage")
	if Page = "" then
		Page = 1
	else
		Page = CLng(Page)
	end if
	if Page < 1 or Page > DocObj.PageCount then
		Response.Expires = 0
		Response.Status = "404 Not Found"
		Response.ContentType = "text/plain"
		Response.Write "Page out of range (IDM Image Services)."
		Response.End
	end if
	PageCount = DocObj.PageCount

	' Report Manager Integration requires retrieving F_DOCFORMAT and F_DOCLOCATION properties
	Set DocProperties   = DocObj.Properties
	if Len(Err.Description) then
		Response.Expires = 0
		Response.Status = "404 Not Found"
		Response.ContentType = "text/plain"
		Response.Write "Properties not found."
		Response.End
	end if

	' Get F_DOCFORMAT property - retrieve by index value
	DocFormatStr = DocProperties.item("F_DOCFORMAT")
	if Len(Err.Description) then
	    DocFormatStr = ""
	end if

	' Get F_DOCLOCATION property - retrieve by index value
	DocLocationStr = DocProperties.item("F_DOCLOCATION")
	if Len(Err.Description) then
		DocLocationString = ""
	end if

else
	'For Document Services, defaults to 1.
	Page=1
	PageCount=1
end if

'Find out whether the document can be annotated or not
DocCanAnnotate = DocObj.GetState(Application("idmDocCanAnnotate"))

'Explicitly set DocCanAnnotate to the value "True" or "False" here instead
'of using CStr() to convert it to a string.  This is due to a difference in
'the German OS where CStr() converts the Boolean value TRUE/FALSE to the
'German word for TRUE/FALSE which causes web viewing to fail.
'This may apply to other non-English OS'es as well.
if DocCanAnnotate then
	DocCanAnnotate = "True"
else
	DocCanAnnotate = "False"
end if

FnWebNavigateVer = "FNWEBNAVIGATE=1.0" + CRLF

ASPSessionId = Request.ServerVariables("HTTP_COOKIE") + CRLF
ApplicationURL = "APPLICATIONURL=" + ServerURL + BaseURL + "/" + CRLF
NavigatorURL = "NAVIGATORURL=" + ServerURL + FullURL + CRLF
ContentURL = "CONTENTURL=" + ServerURL + BaseURL + "/doccontent.dll" + CRLF
PropertiesURL = "PROPERTIESURL=" + ServerURL + BaseURL + "/properties.asp" + CRLF
SecurityURL = "SECURITYURL=" + ServerURL + BaseURL + "/usergrouplist.asp" + CRLF

'TODO -- The following may be changed further !!!
'		 may not need SystemType and LogonID.
'
LibraryName = "LIBRARYNAME=" + LibraryName + CRLF
SystemType = "SYSTEMTYPE=" + CStr(SystemType) + CRLF
LogonId = "LOGONID=" + CStr(LogonId) + CRLF
DocumentID = "DOCUMENTID=" + DocID + CRLF
StartPage = "STARTPAGE=" + CStr(Page) + CRLF
PageCount = "PAGECOUNT=" + CStr(PageCount) + CRLF
DocStateCanAnnotate = "DOCSTATECANANNOTATE=" + DocCanAnnotate + CRLF


'Now, write out the HTTP response.
Response.Expires = -1	'Expires the response immediately

Response.ContentType = "application/x-FileNETNavigate"

Response.Write FnWebNavigateVer

Response.Write ApplicationURL
Response.Write NavigatorURL
Response.Write ContentURL
Response.Write PropertiesURL
Response.Write SecurityURL

Response.Write LibraryName
Response.Write SystemType
Response.Write LogonId
Response.Write DocumentID
Response.Write StartPage
Response.Write PageCount
Response.Write DocStateCanAnnotate

if LEN(DocLocationStr) then
	' Send only if available
	DocLocation = "F_DOCLOCATION=" + DocLocationStr + CRLF
	Response.Write DocLocation
end if

if LEN(DocFormatStr) then
	' Send only if available
	DocFormat   = "F_DOCFORMAT=" + DocFormatStr + CRLF
Response.Write DocFormat
end if

Response.Write ASPSessionId
%>
